package com.hmyd.hmyd_android.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.fragment.GuideFourFragment;
import com.hmyd.hmyd_android.fragment.GuideOneFragment;
import com.hmyd.hmyd_android.fragment.GuideThreeFragment;
import com.hmyd.hmyd_android.fragment.GuideTwoFragment;
//import com.hmyd.hmyd_android.fragment.GuideZeroFragment;

/**
 * 引导页
 * 
 * @author wangc
 * 
 */
public class GuideActivity extends FragmentActivity {
//	private ImageView mIv0;
	private ImageView mIv1;
	private ImageView mIv2;
	private ImageView mIv3;
	private ImageView mIv4;

	private ImageView mIvYun1;
	private ImageView mIvYun2;
	private ImageView mIvYun3;
	private ImageView mIvSun;

	private ViewPager vp;
	private List<Fragment> views;
	private LinearLayout mLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		init();
	}

	private void init() {
//		mIv0 = (ImageView) findViewById(R.id.guide_iv0);
		mIv1 = (ImageView) findViewById(R.id.guide_iv1);
		mIv2 = (ImageView) findViewById(R.id.guide_iv2);
		mIv3 = (ImageView) findViewById(R.id.guide_iv3);
		mIv4 = (ImageView) findViewById(R.id.guide_iv4);

		mIvYun1 = (ImageView) findViewById(R.id.guide_yun1);
		mIvYun2 = (ImageView) findViewById(R.id.guide_yun2);
		mIvYun3 = (ImageView) findViewById(R.id.guide_yun3);
		mIvSun = (ImageView) findViewById(R.id.guide_sun);
		mLayout = (LinearLayout) findViewById(R.id.guide_layout);

		vp = (ViewPager) findViewById(R.id.viewpager);
		views = new ArrayList<Fragment>();
		// 初始化引导图片列表
//		GuideZeroFragment guideZero = new GuideZeroFragment();
		GuideOneFragment guideOne = new GuideOneFragment();
		GuideTwoFragment guideTwo = new GuideTwoFragment();
		GuideThreeFragment guideThree = new GuideThreeFragment();
		GuideFourFragment guideFour = new GuideFourFragment();
		guideFour.setParams(vp, mIvSun, mIvYun1, mIvYun2, mIvYun3);

//		views.add(guideZero);
		views.add(guideOne);
		views.add(guideTwo);
		views.add(guideThree);
		views.add(guideFour);

		initImageView();
		mIv1.setImageResource(R.drawable.yin_1);
		final LinearLayout.LayoutParams params_1 = new LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
		params_1.setMargins(20, 0, 0, 0);

		final LinearLayout.LayoutParams params_2 = new LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 2);
		params_2.setMargins(20, 0, 0, 0);
		mIv1.setLayoutParams(params_2);
		mIv2.setLayoutParams(params_1);
		mIv3.setLayoutParams(params_1);
		mIv4.setLayoutParams(params_1);

		vp.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager()));
		vp.setOffscreenPageLimit(5);

		vp.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {

				LinearLayout.LayoutParams params_1 = new LayoutParams(
						LinearLayout.LayoutParams.WRAP_CONTENT,
						LinearLayout.LayoutParams.WRAP_CONTENT, 1);
				params_1.setMargins(20, 0, 0, 0);
				LinearLayout.LayoutParams params_2 = new LayoutParams(
						LinearLayout.LayoutParams.WRAP_CONTENT,
						LinearLayout.LayoutParams.WRAP_CONTENT, 1);
				params_2.setMargins(0, 0, 0, 0);

				LinearLayout.LayoutParams params_3 = new LayoutParams(
						LinearLayout.LayoutParams.WRAP_CONTENT,
						LinearLayout.LayoutParams.WRAP_CONTENT, 2);
				params_3.setMargins(20, 0, 0, 0);
				LinearLayout.LayoutParams params_4 = new LayoutParams(
						LinearLayout.LayoutParams.WRAP_CONTENT,
						LinearLayout.LayoutParams.WRAP_CONTENT, 2);
				params_4.setMargins(0, 0, 0, 0);
				switch (arg0) {
//				case 0:
//					mLayout.setVisibility(View.VISIBLE);
//					initImageView();
//					mIv0.setImageResource(R.drawable.yin_3);
//					mIv0.setLayoutParams(params_2);
//					mIv1.setLayoutParams(params_1);
//					mIv2.setLayoutParams(params_1);
//					mIv3.setLayoutParams(params_1);
//					mIv4.setLayoutParams(params_1);
//					break;
				case 0:
					mLayout.setVisibility(View.GONE);
					initImageView();
					mIv1.setImageResource(R.drawable.yin_1);
					mIv1.setLayoutParams(params_1);
//					mIv0.setLayoutParams(params_2);
					mIv2.setLayoutParams(params_3);
					mIv3.setLayoutParams(params_1);
					mIv4.setLayoutParams(params_1);
					break;
				case 1:
					mLayout.setVisibility(View.GONE);
					initImageView();
					mIv2.setImageResource(R.drawable.yin_2);
//					mIv0.setLayoutParams(params_2);
					mIv1.setLayoutParams(params_1);
					mIv2.setLayoutParams(params_1);
					mIv3.setLayoutParams(params_3);
					mIv4.setLayoutParams(params_1);
					break;
				case 2:
					mLayout.setVisibility(View.GONE);
					initImageView();
					mIv3.setImageResource(R.drawable.yin_3);
//					mIv0.setLayoutParams(params_2);
					mIv1.setLayoutParams(params_1);
					mIv2.setLayoutParams(params_1);
					mIv3.setLayoutParams(params_3);
					mIv4.setLayoutParams(params_1);
					break;
				case 3:
					mLayout.setVisibility(View.GONE);
					break;

				default:
					break;
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

		TranslateAnimation animationYun1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0f,
				Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT, 0f,
				Animation.RELATIVE_TO_PARENT, 0f);
		animationYun1.setDuration(15000);// 设置动画持续时间
		animationYun1.setFillAfter(true);
		animationYun1.setRepeatCount(100);
		animationYun1.startNow();
		mIvYun1.setAnimation(animationYun1);

		TranslateAnimation animationYun2 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0f,
				Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT, 0f,
				Animation.RELATIVE_TO_PARENT, 0f);
		animationYun2.setDuration(13000);// 设置动画持续时间
		animationYun2.setFillAfter(true);
		animationYun2.setRepeatCount(100);
		animationYun2.startNow();
		mIvYun2.setAnimation(animationYun2);

		TranslateAnimation animationYun3 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0f,
				Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT, 0f,
				Animation.RELATIVE_TO_PARENT, 0f);
		animationYun3.setDuration(10000);// 设置动画持续时间
		animationYun3.setFillAfter(true);
		animationYun3.setRepeatCount(100);
		animationYun3.startNow();
		mIvYun3.setAnimation(animationYun3);
	}

	public class MyViewPagerAdapter extends FragmentStatePagerAdapter {
		public MyViewPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			return views.get(arg0);
		}

		@Override
		public int getCount() {
			return views.size();
		}

		@SuppressWarnings("deprecation")
		@Override
		public Object instantiateItem(View container, int position) {
			return super.instantiateItem(container, position);
		}

		@SuppressWarnings("deprecation")
		@Override
		public void destroyItem(View container, int position, Object object) {
			super.destroyItem(container, position, object);
		}

		@Override
		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}

	}

	private void initImageView() {
//		mIv0.setImageResource(R.drawable.yin_di);
		mIv1.setImageResource(R.drawable.yin_di);
		mIv2.setImageResource(R.drawable.yin_di);
		mIv3.setImageResource(R.drawable.yin_di);

//		android.view.ViewGroup.LayoutParams params0 = mIv0.getLayoutParams();
//		params0.height = 10;
//		params0.width = 10;
//		mIv1.setLayoutParams(params0);
		
		android.view.ViewGroup.LayoutParams params1 = mIv1.getLayoutParams();
		params1.height = 10;
		params1.width = 10;
		mIv1.setLayoutParams(params1);

		android.view.ViewGroup.LayoutParams params2 = mIv2.getLayoutParams();
		params2.height = 10;
		params2.width = 10;
		mIv2.setLayoutParams(params2);

		android.view.ViewGroup.LayoutParams params3 = mIv3.getLayoutParams();
		params3.height = 10;
		params3.width = 10;
		mIv3.setLayoutParams(params3);

		android.view.ViewGroup.LayoutParams params4 = mIv4.getLayoutParams();
		params4.height = 10;
		params4.width = 10;
		mIv4.setLayoutParams(params4);

	}
}
